﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class MainBuy : Form
    {
        UserService userService = new UserService();
        SeatService seatService = new SeatService();
        OrderService orderService = new OrderService();
        StationService stationService = new StationService();
        CarriageService carriageService = new CarriageService();
        TrainSequenceService trainSequenceService = new TrainSequenceService();
        List<Seat> seatsList = new List<Seat>();
        public MainBuy()
        {
            InitializeComponent();
            ShowInTable();
        }

        private void ShowInTable()
        {
            TrainSequence ts = trainSequenceService.FindTrainSequenceByTrainNum(BuyTicketForm.selectedTS);
            dataTable.RowCount = 1;
            this.dataTable[0, 0].Value = ts.TrainNum;
            this.dataTable[1, 0].Value = ts.TrainId;
            this.dataTable[2, 0].Value = stationService.FindStationNameById(ts.StartStation);
            this.dataTable[3, 0].Value = stationService.FindStationNameById(ts.EndStation);
            this.dataTable[4, 0].Value = ts.LaunchTime;
            this.dataTable[5, 0].Value = ts.ArriveTime;
            this.dataTable[6, 0].Value = ts.TravelTime;
            this.dataTable[7, 0].Value = carriageService.FindHighSeatNumByTrainId(ts.TrainId);
            this.dataTable[8, 0].Value = carriageService.FindLowSeatNumByTrainId(ts.TrainId);
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MainBuy_FormClosed(object sender, FormClosedEventArgs e)
        {
            BuyTicketForm.buyTicketForm.Show();
        }

        private void btnPurchase_Click(object sender, EventArgs e)
        {
            User u = userService.FindUserByAccount(LoginFrom.userAccount);
            if(textBoxCertificateNum.Text != null && textBoxCertificateNum.Text.Trim() == u.CertificateNum && checkedListBoxCarrige.SelectedIndex != -1)
            {
                BuyTicket(u);
                MessageBox.Show("购票成功，已自动退出");
                this.Close();
                return;
            }

            MessageBox.Show(textBoxCertificateNum.Text == null ? "未填入身份证号进行验证" :
                textBoxCertificateNum.Text.Trim() != u.CertificateNum ? "身份证号验证失败" :
                checkedListBoxCarrige.SelectedIndex == -1 ? "未选择车座档次": "" );
        }

        private void BuyTicket(User u)
        {
            TrainSequence ts = trainSequenceService.FindTrainSequenceByTrainNum(BuyTicketForm.selectedTS);
            int SeatId = seatService.BuySuitableSeat(checkedListBoxCarrige.SelectedIndex,ts.TrainId);
            Order order = new Order(1, u.Id,SeatId,ts.TrainSequenceId,DateTime.Now);
            orderService.AddOrder(order);
        }
    }
}
